package codec;

import command.SetCommand;
import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

/**
 * Created with IntelliJ IDEA.
 * User: zhangyong
 * Date: 13-7-23
 * Time: 下午10:00
 * To change this template use File | Settings | File Templates.
 */
public class CommandEncoder extends ProtocolEncoderAdapter {

    private static Logger logger = Logger.getLogger(CommandEncoder.class);

    /*
    先调用 encode....然后是 filter 再然后 handler
    */
    public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
        logger.info("encoding....");
        if (message instanceof SetCommand) {
            logger.info("发送的是setCommand");
            SetCommand command = (SetCommand) message;
            if (command.getIoBuffer() == null) {
                command.encode();
            }
            out.write(command.getIoBuffer());
        }
    }

}
